
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/login/model/models.dart';
import 'package:nft_shop/my/page/set/my_authentication_page.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/click_item.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/select_image.dart';
import 'package:nft_shop/widgets/text_field_item.dart';

class MyInfoDataPage extends StatefulWidget {
  const MyInfoDataPage({Key? key}) : super(key: key);

  @override
  _MyInfoDataPageState createState() => _MyInfoDataPageState();
}

class _MyInfoDataPageState extends State<MyInfoDataPage> {
  final TextEditingController nickNameController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final TextEditingController _controller2 = TextEditingController();
  final FocusNode _nodeText2 = FocusNode();

  String urlStr = "";

  @override
  Widget build(BuildContext context) {
    urlStr = UserController.to.isLogin
        ? UserController.to.member.portrait!
        : "";
    nickNameController.text = UserController.to.isLogin ? UserController.to.member.nickname! : "";
    _controller2.text = UserController.to.isLogin ? UserController.to.member.introduce! : "";
    String realNameStatus = "";
    //是否完成身份认证 0未提交 1待审核 2审核通过 3驳回
    if(UserController.to.member.realNameStatus == "0"){
      realNameStatus = "未实名";
    }else if(UserController.to.member.realNameStatus == "1"){
      realNameStatus = "审核中";
    }else if(UserController.to.member.realNameStatus == "2"){
      realNameStatus = "已实名";
    }else if(UserController.to.member.realNameStatus == "3"){
      realNameStatus = "驳回";
    }
    print("身份认证状态---${UserController.to.member.realNameStatus}");

    return InkWell(
      onTap: (){FocusScope.of(context).requestFocus(new FocusNode());},
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "个人信息",
        ),
        body: MyScrollView(
          children: [
            Stack(
              children: [
                ClickItem(
                  title: "头像",
                ),
                Positioned(
                  right: 15,
                  top: 10,
                  bottom: 10,
                  child: SelectedImage(
                    url:urlStr,
                    determineBack: (String url){
                      urlStr = url;
                    },
                  ),
                ),
              ],
            ),
            ClickItem(
              title: "实名信息",
              content: realNameStatus,
              onTap: (realNameStatus == "审核中" || realNameStatus == "已实名") ?(){
                if(realNameStatus == "审核中"){
                  EasyLoading.showToast("存在待审核申请,请等待审核结果");
                }
              }:() => Get.to(MyAuthenticationPage()),
            ),
            TextFieldItem(
              title: '昵称',
              hintText: '请输入用户昵称',
              controller: nickNameController,
              focusNode: _nodeText1,
              contentTextStyle: TextStyle(color: Color(0xFF131415),fontSize: 15,fontFamily: "PingFangSC-Semibold"),
            ),
            ClickItem(title: "手机号",content: UserController.to.isLogin ?UserController.to.member.mobile!:""),
            ClickItem(
                title: "注册时间",
                content: UserController.to.isLogin ?UserController.to.member.createTime!:""),
            introductionView(),
            SizedBox(
              height: 46,
            ),
            Container(
              height: 40,
              margin: EdgeInsets.symmetric(horizontal: 15),
              decoration: BoxDecoration(
                // Colours.app_main,
                borderRadius: BorderRadius.circular(5),
              ),
              child: MyButton(
                key: const Key('login'),
                minHeight: 40,
                onPressed: () async{
                  _nodeText1.unfocus();
                  _nodeText2.unfocus();

                  if (urlStr.length == 0 ) {
                    EasyLoading.showToast('请上传头像');
                    return;
                  }

                  if (nickNameController.text.length == 0) {
                    EasyLoading.showToast('请上输入昵称');
                    return;
                  }

                  if (nickNameController.text.length > 16) {
                    EasyLoading.showToast('昵称不能大于16位');
                    return;
                  }

                  if (_controller2.text.length == 0) {
                    EasyLoading.showToast('请输入简介');
                    return;
                  }

                  EasyLoading.show(status: '加载中...');
                  try{
                    await ServiceRepository.setMemberInfo(urlStr,nickNameController.text,_controller2.text);

                    Member model = UserController.to.member;
                    model.nickname = nickNameController.text;
                    model.portrait = urlStr;
                    model.introduce = _controller2.text;
                    UserController.to.saveUser(model);
                    EasyLoading.showToast("成功");
                    Get.back();
                  }catch(e,s){
                    EasyLoading.showToast(http.setError(e, s));
                  }
                },
                textColor: Colors.white,
                backgroundColor: Global.kTheme.colorBackground3,
                text: "提交",
                fontSize: 16,
              ),
            ),
          ],
        ),
      ),
    );
  }

  //简介
  Widget introductionView(){
    return Container(
      height: 170,
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 15),
            child: Text("简介",style: TextStyle(fontSize: 14,color: Global.kTheme.textPrimary6),),
          ),
          SizedBox(width: 30,),
          Expanded(
            child: TextField(
              controller: _controller2,
              focusNode: _nodeText2,
              keyboardType: TextInputType.multiline,
              maxLines: null, //不限制行数
              decoration: InputDecoration(
                labelStyle:TextStyle(fontSize: 14,color: Global.kTheme.textPrimary12,fontFamily: Global.mySemiboldFont),
                hintText: "请简单的自我介绍一下，可以让更多的用户了解您哦",
                border: InputBorder.none, // 去掉下滑线
              ),
            ),
          ),
        ],
      ),
    );
  }

}

